package com.alibaba.dubbo.spi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * @author zhangjuntao
 * @version V1.0
 * @Title  加载所有SPI的类的拓展类
 * @Description ${todo}
 * @date 2017-11-05 下午9:20
 */
public class SPIExtensionFactory {

    private static final Logger LOGGER = LoggerFactory.getLogger(SPIExtensionFactory.class);

    public static <T> T newInstance(Class<T> clazz) {
        T instance = null;
        ServiceLoader<T> serviceLoader = ServiceLoader.load(clazz);
        Iterator<T> instances = serviceLoader.iterator();
        while (instances.hasNext()) {
            instance = instances.next();
            LOGGER.info("{} 的实现类：{}",clazz,String.valueOf(instance));
        }
        return instance;
    }
}
